package com.attendcollatesys.util;

import com.attendcollatesys.entity.WorkDay;
import com.attendcollatesys.ex.handler.ex.WorkDayDataBaseInitException;
import com.attendcollatesys.service.WorkDayService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class HolidayApiUtil {
    @Autowired
    WorkDayService workDayService;

    //第三方api 获取工作日 接口说明地址http://doc.apihubs.cn/
    private static final String HOLIDAY_API_URL = "https://api.apihubs.cn/holiday/get";
    private static final String API_KEY="de96811d03042042d11962e1484ec4201992";

    public void setHolidayDate() throws Exception {

        ArrayList<WorkDay> workDays = new ArrayList<>();
        WorkDay workDay;
        RestTemplate restTemplate = new RestTemplate();
        Calendar calendar = Calendar.getInstance();
        String currentYear=String.valueOf(calendar.get(Calendar.YEAR));

        try {
            for (int i = 1; i <= 12; i++) {
                String url = HOLIDAY_API_URL + "?year=" + currentYear + "&month=" + currentYear+(i<10?"0"+i:i) + "&workday=1"+"&api_key="+API_KEY;

                //RequestEntity<Void> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, URI.create(url));
                ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
                log.debug(url);
                //ResponseEntity<String> response = restTemplate.exchange(requestEntity, String.class);
                String json = response.getBody();
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode rootNode = objectMapper.readTree(json);
                JsonNode listNode = rootNode.get("data").get("list");
                if(listNode==null||listNode.size()==0){
                    throw new WorkDayDataBaseInitException("工作日的获取为空 请查询相关api请求是否生效--->"+url);
                }

                List<String> dateList = new ArrayList<>();
                for (JsonNode node : listNode) {
                    String date = node.get("date").asText();
                    dateList.add(date);
                }

                workDay = new WorkDay();
                workDay.setYearMonthKey(currentYear + (i<10?"0"+i:i))
                        .setWorkDay(dateList.stream()
                                .map(o->o.substring(6))
                                .collect(Collectors.joining(",")));
                workDays.add(workDay);

            }


            workDayService.insertBatch(workDays);
        }catch (DuplicateKeyException ex){
            log.debug("已初始化过工作日相关表");
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new WorkDayDataBaseInitException("工作日的获取或者相关表初始化失败"+e);
        }

// 输出结果
        //System.out.println(dateList);


    }


}
